package com.xja.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xja.model.entity.Question;
import com.xja.model.vo.resp.Result;
import com.xja.service.IQuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 问题表--用于体质评估 前端控制器
 * </p>
 *
 * @author tanggy
 * @since 2025-06-11
 */
@RestController
@RequestMapping("/admin/questions")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "问卷模块")
public class AdminQuestionController {

    private final IQuestionService questionService;

    //获取问卷问题列表
    @GetMapping("/search")
    @Operation(summary = "获取问题列表")
    public Result<Page<Question>> getQuestionList(String keyword,
                                  @DefaultValue("1") Long pageNum,
                                  @DefaultValue("10") Long pageSize) {
        Page<Question> p = questionService.search(keyword, pageNum, pageSize);
        return Result.success(p);
    }

    //添加新问题
    @PostMapping
    @Operation(summary = "添加问题")
    public Result<Void> addQuestion(@RequestBody Question question) {
        questionService.save(question);
        return Result.success();
    }

    //删除问题
    @DeleteMapping("/{id}")
    @Operation(summary = "删除问题")
    public Result<Void> deleteQuestion(@PathVariable("id") Integer id) {
        boolean isDelete = questionService.removeById(id);
        if (!isDelete) {
            return Result.error("删除成功");
        }
        return Result.success();
    }

    //修改问题
    @PutMapping
    @Operation(summary = "更新问题")
    public Result<Void> updateQuestion(@RequestBody Question question) {
        boolean isUpdate = questionService.updateById(question);
        if (!isUpdate) {
            return Result.error("更新失败");
        }
        return Result.success();
    }
}